package com.wmh.baseservice.admin.controller;


import com.alibaba.fastjson.JSONObject;
import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.utils.CheckUtil;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ResultCodeInfo;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.user.pojo.UsAddress;
import com.wmh.baseservice.user.pojo.UsGoodsOrder;
import com.wmh.baseservice.user.pojo.UsInfo;
import com.wmh.baseservice.user.service.UsAddressService;
import com.wmh.baseservice.user.service.UsGoodsOrderService;
import com.wmh.baseservice.user.service.UsInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@Api(tags = "管理员端-商品订单相关接口")
@RestController
@RequestMapping(value = "admin/adGoodsOrder", produces = "text/plain;charset=utf-8")
@RequiresRoles("admin")
public class AdGoodsOrderController {
    @Resource
    private UsGoodsOrderService usGoodsOrderService;
    @Resource
    private UsInfoService usInfoService;
    @Resource
    private UsAddressService usAddressService;

    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    public String list(PageInfo pageInfo, String orderNum, Long usId, Integer status, Integer type) {
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort("create_time", SortWay.DESC);
        //执行分页辅助工具

        PageResult<UsGoodsOrder> pageResult = new PageUtil<UsGoodsOrder>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(orderNum)) {
                queryWrapper.eq(UsGoodsOrder.ORDER_NUM, orderNum);
            }
            if (CommUtil.checkNull(usId)) {
                queryWrapper.eq(UsGoodsOrder.US_ID, usId);
            }
            if (CommUtil.checkNull(status)) {
                queryWrapper.eq(UsGoodsOrder.STATUS, status);
            }
            if (CommUtil.checkNull(type)) {
                queryWrapper.eq(UsGoodsOrder.TYPE, type);
            }
            queryWrapper.select(UsGoodsOrder.getFields(UsGoodsOrder.SNAPSHOT));
            usGoodsOrderService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("详情")
    @PostMapping("detail")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    @ParameterVerify(notNull = "id")
    public String detail(Long id) {
        JSONObject object = new JSONObject(true);
        UsGoodsOrder usGoodsOrder = usGoodsOrderService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(usGoodsOrder)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        UsAddress usAddress = usAddressService.getByIdForUpdate(usGoodsOrder.getAddressId());
        if (CommUtil.checkNull(usAddress)) {
            object.put("address", usAddress);
        }
        UsInfo usInfo = usInfoService.getByIdForUpdate(usGoodsOrder.getUsId());
        if (CommUtil.checkNull(usInfo)) {
            object.put("nick", usInfo.getNick());
            object.put("avatar", usInfo.getAvatar());
        }
        object.put("goodsOrder", usGoodsOrder);
        return ReturnBody.success(object);
    }
    @ApiOperation("发货")
    @PostMapping("delivery")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = false),
    })
    @ParameterVerify(notNull = "id")
    public String delivery(Long id,String expressNumber,Integer type) {
        UsGoodsOrder usGoodsOrder = usGoodsOrderService.getByIdForUpdate(id);
        if (!CommUtil.checkNull(usGoodsOrder)) {
            return ReturnBody.error(ResultCodeInfo.PARAM_ERROR);
        }
        if (usGoodsOrder.getStatus() != 1) {
            return ReturnBody.error("状态错误");
        }
        usGoodsOrder.setStatus(2);
        if (type==1){
            if (!CommUtil.checkNull(expressNumber)){
                return ReturnBody.error("快递单号不可为空");
            }
            usGoodsOrder.setExpressNumber(expressNumber);
        }
        usGoodsOrderService.delivery(usGoodsOrder);
        return ReturnBody.success();
    }

}
